package org.itboys.core.controller.web.company;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.tray.SundayTrayApplyDto;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mongo.SundayTrayApply;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.service.mongo.web.SundayTrayApplyService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2018/12/15
 * 描述：供应商托盘
 */
@Controller
@RequestMapping("/sunday/web/company/apply")
@Api(value = "apply", description = "供应商托盘投放")
public class SundayCompanyTrayApplyController extends BaseController {
    @Autowired
    private SundayTrayApplyService applyService;
    @Autowired
    private SundayMemberService memberService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void apply(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "模板id") @RequestParam(value = "modelId", required = true) Long modelId,
            @ApiParam(value = "仓库id") @RequestParam(value = "storeId", required = true) Long storeId,
            @ApiParam(value = "数量") @RequestParam(value = "num", required = true) Long num
    ) {
        SundayMember member=memberService.getMemberByUserId(AdminSessionHolder.getAdminUserId());
        applyService.save(new SundayTrayApply(member.getId(), modelId, storeId, num, SundayCommonConstants.tray_apply_status_ing));
        ResponseMessage.success();
    }



    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model
    ) {
        SundayMember member=memberService.getMemberByUserId(AdminSessionHolder.getAdminUserId());
        model.addAttribute("member", member);
        return "/sunday/company/tray/apply/index";
    }


    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            Model model
    ) {
        SundayMember member=memberService.getMemberByUserId(AdminSessionHolder.getAdminUserId());
        model.addAttribute("member", member);
        return "/sunday/company/tray/apply/input";
    }

    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        PageResult<SundayTrayApplyDto> result = applyService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }








}
